package com.xxwy.dependency.injection.setting;

import com.xxwy.dependency.UserHold;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author wy
 * @date 2020/4/26 14:02
 */
public class AnnotationAndAPIDependencySetterInjection {

  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(AnnotationConfigApplicationContext.class);

    XmlBeanDefinitionReader definitionReader = new XmlBeanDefinitionReader(context);
    int definitions = definitionReader.loadBeanDefinitions("classpath:/META-INF/dependency-lookup-context.xml");

    context.refresh();

    BeanDefinition beanDefinition = createUserHoleApi();
    context.registerBeanDefinition("userHold",beanDefinition);

    UserHold bean = context.getBean(UserHold.class);
    System.out.println(bean);

    context.close();
  }

  /**
   * 手动注解依赖查找
   */
  /*@Bean
  public UserHold userHold(User user){
    return new UserHold(user);
  }
  */

  /**
   * 手动api
   * @return
   */
  private static BeanDefinition createUserHoleApi(){
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(UserHold.class);
    builder.addPropertyReference("user","user");
    builder.addConstructorArgReference("user");
    return builder.getBeanDefinition();
  }
}
